// 学生服务接口
package org.example.backend.service;

import org.example.backend.dto.StudentDto;
import org.example.backend.entity.Student;
import org.example.backend.entity.User;
import org.example.backend.exception.BusinessException;
import org.example.backend.exception.ResourceNotFoundException;
import org.example.backend.repository.StudentRepository;
import org.example.backend.repository.UserRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class StudentService {

    private final StudentRepository studentRepository;
    private final UserRepository userRepository;

    public StudentService(StudentRepository studentRepository, UserRepository userRepository) {
        this.studentRepository = studentRepository;
        this.userRepository = userRepository;
    }

    /**
     * 创建学生信息
     */
    public Student createStudent(StudentDto studentDto, Long userId) {
        // 检查学号是否已存在
        if (studentRepository.findByStudentNumber(studentDto.getStudentNumber()).isPresent()) {
            throw new BusinessException("学号已存在");
        }

        // 检查用户是否存在
        User user = userRepository.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("用户不存在"));

        // 检查是否已有关联的学生信息
        if (studentRepository.findByUserId(userId).isPresent()) {
            throw new BusinessException("该用户已有关联的学生信息");
        }

        Student student = new Student();
        student.setUserId(userId);
        student.setStudentNumber(studentDto.getStudentNumber());
        student.setName(studentDto.getName());
        student.setGender(studentDto.getGender());
        student.setBirthDate(studentDto.getBirthDate());
        student.setMajor(studentDto.getMajor());
        student.setGrade(studentDto.getGrade());
        student.setClassName(studentDto.getClassName());
        student.setAvatar(studentDto.getAvatar());
        student.setResume(studentDto.getResume());

        return studentRepository.save(student);
    }

    /**
     * 更新学生信息
     */
    public Student updateStudent(Long studentId, StudentDto studentDto) {
        Student student = studentRepository.findById(studentId)
                .orElseThrow(() -> new ResourceNotFoundException("学生信息不存在"));

        // 检查学号是否已被其他学生使用
        Student existingStudent = studentRepository.findByStudentNumber(studentDto.getStudentNumber()).orElse(null);
        if (existingStudent != null && !existingStudent.getId().equals(studentId)) {
            throw new BusinessException("学号已存在");
        }

        student.setStudentNumber(studentDto.getStudentNumber());
        student.setName(studentDto.getName());
        student.setGender(studentDto.getGender());
        student.setBirthDate(studentDto.getBirthDate());
        student.setMajor(studentDto.getMajor());
        student.setGrade(studentDto.getGrade());
        student.setClassName(studentDto.getClassName());
        student.setAvatar(studentDto.getAvatar());
        student.setResume(studentDto.getResume());

        return studentRepository.save(student);
    }

    /**
     * 获取学生信息
     */
    @Transactional(readOnly = true)
    public Student getStudentByUserId(Long userId) {
        return studentRepository.findByUserId(userId)
                .orElseThrow(() -> new ResourceNotFoundException("学生信息不存在"));
    }

    /**
     * 获取学生信息
     */
    @Transactional(readOnly = true)
    public Student getStudentById(Long studentId) {
        return studentRepository.findById(studentId)
                .orElseThrow(() -> new ResourceNotFoundException("学生信息不存在"));
    }

    /**
     * 管理员更新学生信息
     */
    public Student updateStudentByAdmin(Long userId, StudentDto studentDto) {
        Student student = studentRepository.findByUserId(userId)
                .orElseThrow(() -> new ResourceNotFoundException("学生信息不存在"));

        // 检查学号是否已被其他学生使用
        Student existingStudent = studentRepository.findByStudentNumber(studentDto.getStudentNumber()).orElse(null);
        if (existingStudent != null && !existingStudent.getId().equals(student.getId())) {
            throw new BusinessException("学号已存在");
        }

        student.setStudentNumber(studentDto.getStudentNumber());
        student.setName(studentDto.getName());
        student.setGender(studentDto.getGender());
        student.setBirthDate(studentDto.getBirthDate());
        student.setMajor(studentDto.getMajor());
        student.setGrade(studentDto.getGrade());
        student.setClassName(studentDto.getClassName());
        student.setAvatar(studentDto.getAvatar());
        student.setResume(studentDto.getResume());

        return studentRepository.save(student);
    }

}
